package avgCitySalary;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Iterator;

public class AvgSalaryReducer extends Reducer<Text, DoubleWritable, Text, Text> {
    private Text result = new Text();

    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        double sum = 0;
        int count = 0;
        for (DoubleWritable value : values) {
            sum += value.get();
            count++;
        }
          //保留俩位小数！！！！
        double average = sum / count;
        // 使用String.format方法格式化数字，保留两位小数，并拼接单位"万"
       // String formattedAverage = String.format("%.2f万", average);
        //result.set(formattedAverage);
        //context.write(key, result);

        result.set(String.valueOf(sum / count)+"万"); // 计算平均值
        context.write(key,result);
    }
}